package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.ZtfAppLearningRecord;
import com.ruoyi.system.domain.ZtfAppVideoTable;
import com.ruoyi.system.domain.ZtfLearningRecord;
import com.ruoyi.system.domain.ZtfVideoTable;
import com.ruoyi.system.mapper.ZtfAppLearningRecordMapper;
import com.ruoyi.system.mapper.ZtfAppVideoTableMapper;
import com.ruoyi.system.mapper.ZtfLearningRecordMapper;
import com.ruoyi.system.mapper.ZtfVideoTableMapper;
import com.ruoyi.system.service.IZtfAppLearningRecordService;
import com.ruoyi.system.service.IZtfLearningRecordService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 课程记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-06
 */
@Service
public class ZtfAppLearningRecordServiceImpl implements IZtfAppLearningRecordService
{
    @Autowired
    private ZtfAppLearningRecordMapper ztfAppLearningRecordMapper;

    @Autowired
    private ZtfAppVideoTableMapper ztfAppVideoTableMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 新增课程记录
     * 
     * @param ztfAppLearningRecord 课程记录
     * @return 结果
     */
    @Override
    public int insertZtfLearningRecord(ZtfAppLearningRecord ztfAppLearningRecord) {
        //查询相同视频的播放记录
        List<ZtfAppLearningRecord> ztfAppLearningRecordList=ztfAppLearningRecordMapper.selectRecordList(ztfAppLearningRecord.getVideoId());
        //获取该视频所有播放记录中的最大时长
        if(ztfAppLearningRecordList.size()>0){
            OptionalDouble maxStartTimeBasic = ztfAppLearningRecordList.stream()
                    .mapToDouble(record -> Double.parseDouble(record.getStartTime()))
                    .max();
            //将最大播放时长进行转换
            Double maxStartTimeBasic1=maxStartTimeBasic.getAsDouble();
            int isOver=0;
            ZtfAppVideoTable ztfAppVideoTable = ztfAppVideoTableMapper.selectZtfVideoTableById(ztfAppLearningRecord.getVideoId());
            //获取视频的总时长
            if (ztfAppVideoTable != null) {
                String totalHourStr = ztfAppVideoTable.getDuration();
                //获取新的播放时长
                String newStartTimeStr = ztfAppLearningRecord.getStartTime();
                if (totalHourStr != null && newStartTimeStr != null) {
                    int totalHour = Integer.parseInt(totalHourStr);
                    //新的播放进度转换
                    Double newStartTime = Double.parseDouble(newStartTimeStr);
                    //查询旧的最大播放进度
                    Double oldVideoRate =maxStartTimeBasic1 / totalHour * 100;
                    //新的播放进度计算
                    Double newVideoRate = newStartTime / totalHour * 100;
                    //新旧播放进度做对比
                    newVideoRate=oldVideoRate>newVideoRate ? oldVideoRate:newVideoRate;
                    //进度为95就相当于100
                    if(newVideoRate>95){
                        newVideoRate=100.0;
                        isOver=1;
                    }
                    //播放进度取整
                    int roundedPercentage = (int) Math.floor(newVideoRate);
                    String video_rateStr = String.valueOf(roundedPercentage);
                    //播放进度设置
                    ztfAppVideoTable.setViews(video_rateStr);
                    //播放满100为已学完

                    ztfAppVideoTable.setIsOver(isOver);
                    //更新播放进度
                    ztfAppVideoTableMapper.updateZtfVideoTable(ztfAppVideoTable);
                }
            }
        }

        //设置创建时间
        ztfAppLearningRecord.setCreateTime(new Date());
        //设置修改时间
        ztfAppLearningRecord.setUpdateTime(new Date());
        return ztfAppLearningRecordMapper.insertZtfLearningRecord(ztfAppLearningRecord);
    }

}
